<?php
spl_autoload_register('namespace_library_loader');
function namespace_library_loader($class) {
	$dir = './';
	set_include_path(get_include_path().PATH_SEPARATOR.$dir);
	$class = str_replace('\\', '\/', $class) . '.class.php';
	require_once($class);
}

const CLOUD_KEY = ''; //高德云图KEY
$cloud_key = (isset($_REQUEST['cloud_key']) && $_REQUEST['cloud_key']) ? $_REQUEST['cloud_key'] : CLOUD_KEY;

global $gaode;
$gaode = new \Common\Org\Util\GaodeAmapLbs($cloud_key);
$cloud_key = $gaode->getCloudKey();

define( 'SELF_NAME', basename($_SERVER['PHP_SELF']) );

if(isset($_REQUEST['do']) && $_REQUEST['do']=='make') {
	$gps_x = isset($_REQUEST['gps_x']) ? $_REQUEST['gps_x'] : '';
	$gps_y = isset($_REQUEST['gps_y']) ? $_REQUEST['gps_y'] : '';
	$gps = getGCJ02($gps_x, $gps_y);
	if($gps && $gps[0]) {
		$str = "转换后的火星坐标经纬度： ".$gps[0]. ", " .$gps[1];
	}
	else {
		$str = "获取信息失败！";
	}
	echo $str;
	exit();
}

//原始坐标器转为火系坐标系
function getGCJ02($gps_x, $gps_y) {
	$lng = GPSToLatLng($gps_x);
	$lat = GPSToLatLng($gps_y);
	$res_lng = $res_lat = '';
	//高德坐标转换
	global $gaode;
	$response = $gaode->convertLatLng( [ $lng. ',' . $lat ] );
	if($response['status']==0 && $response['data']) {
		$lnglat = $response['data'][0];
		$res_lng = substr($lnglat, 0, strpos($lnglat, ','));
		$res_lat = substr($lnglat, strpos($lnglat, ',')+1);
	}
	return array($res_lng, $res_lat);
}

/**
 * 度分秒轉經緯度
 * @param string $text
 * @return float
 */
function GPSToLatLng($text) {
	$result = "";
	$text = strtr(trim($text), ['゜' => '°', '\'' => '′', '"' => '″']);

	//必須有度分秒才可進行轉換
	if (stripos($text, '°') && stripos($text, '′') && stripos($text, '″')) {
		//取得度分秒
		$dArr = explode("°", $text);
		$mArr = explode("′", $dArr[1]);
		$sArr = explode("″", $mArr[1]);

		if (!empty($dArr) && !empty($mArr) && !empty($sArr)) {
			$degree = $dArr[0];
			$minute = $mArr[0];
			$second = $sArr[0];
			//x度 y分 z秒 = x + y/60 + z/3600 度
			$result = strval($degree + ($minute / 60) + ($second / 3600));
		}
	}
	return round($result, 6);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<base href="./" />
	<meta http-equiv="content-type" content="text/html; charset=utf-8" />
	<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
	<title>原始坐标体系（地球坐标、GPS坐标、WGS-84坐标）转火星坐标（GCJ-02）</title>
	<script src="statics/Js/jquery.min.js"></script>
	<link rel="stylesheet" type="text/css" href="statics/Css/api-index.css" />
</head>
<body>
<style type="text/css">
	.input-text { width:400px; }
	.input-text2 { width:250px; }
	h3{ font-size:12px; }
	h2{ font-size:14px; }
</style>

<body style="margin-left:3px;">


<br><hr width="600" align=left><!----------------------------->

<div class="main" style="">
	<h2>原始坐标（地球坐标、GPS坐标）转火星坐标（WGS-84）</h2>
	<h3>注： Google坐标和高德地图坐标均为火星坐标体系</h3>
	<div class="block-msg">
		<form method="post" >
			<table width="100%">
				<tr>
					<td align="right" width="150" height="35">高德云图KEY：</td>
					<td><input type="text" id="cloud_key" name="cloud_key" value="<?echo $cloud_key;?>" class="input-text" />
						&nbsp;&nbsp;<a href="http://lbs.amap.com/yuntu/reference/cloudstorage/" target="_blank" style="color: red;">申请KEY</a>
					</td>
				</tr>
			
				<tr>
					<td align="right" width="150" height="35">原始坐标系（经）：</td>
					<td><input type="text" id="gps_x" name="gps_x" value="110°41′70″" class="input-text2" /></td>
				</tr>

				<tr><td align="right" width="150" height="35">原始坐标系（纬）：</td><td>
						<input type="text" name="gps_y" id="gps_y" value="21°19′21″" class="input-text2" ></td></tr>

				<tr>
					<td  align="right" height="35"> </td>
					<td><input type="button" class="button" value="生成数据" onclick="makeDo(this)" /></td>
				</tr>
			</table>

			<table width="100%" style="display:none" id="j_TestArea">
				<tr>
					<td>
						<div style="margin-left:20px;">
							<textarea cols=80 rows=4 name="txt_TestArea" id="txt_TestArea"></textarea>
						</div>
					</td>
				</tr>
			</table>

		</form>
	</div>
</div>
<script type="text/javascript">
	//生成JSON:
	function makeDo(e) {
		var form = $(e).parents('form');
		var data = {
			cloud_key: $('#cloud_key',form).val(),
			gps_x: $('#gps_x',form).val(),
			gps_y: $('#gps_y',form).val()
		};
		makeDo_Common(form, data);
	}

	//生成JSON:
	function makeDo_Common(form, data) {
		$.ajax( {
			url:"<?php echo SELF_NAME;?>?do=make",// 发送 action
			data:data,
			type:'post',
			cache:false,
			dataType:'text',
			success:function(data) {
				$('#txt_TestArea',form).val(data);
			},
			error : function() {
				//alert("失败！");
			}
		});
		$('#j_TestArea',form).show();
	}
</script>

<br><hr width="600" align=left><!----------------------------->

<br /><br /><br /><br /><br /><br />
</body>
</html>
